﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Xml.Serialization;
using System.IO;
//using System.Windows.Forms;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

// GetTPB, Copyright Mirosław M. Mączka.  All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
namespace GetTpb
{


    public class Config
    {
        private static string cfgFile = "GetTpb.xml";
        [Serializable]
        public class Cfg
        {
            public TpbSource[] Src;
        }

        public static void Save(List<TpbSource> lis)
        {
            try
            {
                TpbSource[] src = lis.ToArray<TpbSource>();
                XmlSerializer ser = new XmlSerializer(typeof(Cfg));
                Cfg cfg = new Cfg();
                cfg.Src = src;
                using (TextWriter writer = new StreamWriter(cfgFile))
                {
                    ser.Serialize(writer, cfg);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        public static void Read(ref List<TpbSource> lis)
        {
            Cfg cfg = new Cfg();
            //
            try
            {
                XmlSerializer ser = new XmlSerializer(typeof(Cfg));
                using (TextReader reader = new StreamReader(cfgFile))
                {
                    cfg = (Cfg)ser.Deserialize(reader);
                }
                //
                lis.Clear();
                lis = cfg.Src.ToList<TpbSource>();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            //
        }

        public static void WriteHelp()
        {
            using (TextWriter writer = new StreamWriter(cfgFile, true, Encoding.UTF8))
            {
                string sh="\n<!--\n<webPage>adres strony www</webPage>\n<Title>nazwa, także nazwa tworzonego pliku</Title>\n<minSize>minimalna wielkość ściąganych plików w MB, 0 - wszystkie wielkości</minSize>\n<maxCount>największa ilość odczytanych pozycji 0 - wszystkie dostępne</maxCount>\n<Selection>wybierać tylko pozycje zawierające podany tekst, puste - wszystkie</Selection>\n</TpbSource>\n-->";
                writer.WriteLine(sh);
            }
        }

        public static void ReadOrCreate(ref List<TpbSource> lis)
        {
            if (!File.Exists(cfgFile))
            {
                Save(lis); WriteHelp();
            }
            else
            {
                Read(ref lis);
            }
        }

        public static void Default(List<TpbSource> lis)
        {
            lis.Add(new TpbSource("http://thepiratebay.org/browse/104", "FLAC", 0, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/204", "Klipy video", 300, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/203", "Teledyski", 0100, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/202", "Filmy DVD", 0, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/207", "Highres Movies", 0, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/201", "Filmy", 0, 200, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/browse/101", "Muzyka", 0, 600, ""));
            lis.Add(new TpbSource("http://thepiratebay.org/search/discography/0/99/100", "Dyskografie", 0, 200, ""));
        }

    }//class
}//namespace
